package com.ruoyi.workflow.context;

import com.ruoyi.workflow.service.IFlowableCallbackService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class FlowableCallbackContext{

    //方法1:实现ApplicationContextAware,通过上下文去获取指定Service的实现类
    //private Map<String, IFlowableCallbackService> flowableCallbackServiceMap = new ConcurrentHashMap<>();

    //方法2:【推荐】该map通过自动注入就是key是实现类的名字,value是实现类的实例
    @Autowired
    private Map<String, IFlowableCallbackService> flowableCallbackServiceMap;


    /**
     * 获取资源，里面其实就是根据服务名去取出对应的业务实现类
     *
     * @param serviceName
     * @return
     */
    public IFlowableCallbackService getResource(String serviceName) {
        return flowableCallbackServiceMap.get(serviceName);
    }

    /**
     * 方法1:实现 implements ApplicationContextAware
     * @param applicationContext
     * @throws BeansException
     */
    /*@Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, IFlowableCallbackService> beanMap = applicationContext.getBeansOfType(IFlowableCallbackService.class);
        beanMap.forEach((k, v)-> this.flowableCallbackServiceMap.put(k, v));
        //或者遍历该接口的所有实现，将其放入map中
        *//*for (IFlowableCallbackService serviceImpl : beanMap.values()) {
            flowableCallbackServiceMap.put(serviceImpl.getClass().getSimpleName(), serviceImpl);
        }*//*
    }*/
}
